#include <QtWidgets>
#include <QDebug>
#include "cg_grandtapis.h"
#include "cg_autre.h"

Cg_GrandTapis::Cg_GrandTapis() : m_numeroJoueur(0), m_joueurMaitre(0), m_nomJoueur("Bob"), m_message("")
{

}

QRectF Cg_GrandTapis::boundingRect() const
{
    return QRectF(0, 0, 1920, 1080);
}

void Cg_GrandTapis::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    qDebug()<<"Paint - GrandTapis\n";
    Q_UNUSED(option);
    Q_UNUSED(widget);
    QFont f1("Andalus");
    f1.setPointSize(40);
    painter->setFont(f1);
    painter->drawText(QPointF(1510,340),m_noms[1]);
    painter->drawText(QPointF(1240,120),m_noms[2]);
    painter->drawText(QPointF(390,130),m_noms[3]);
    painter->drawText(QPointF(10,390),m_noms[4]);

    painter->drawPixmap(QPointF(1500,856), m_pli[0]);
    painter->drawPixmap(QPointF(1568,856), m_pli[1]);
    painter->drawPixmap(QPointF(1636,856), m_pli[2]);
    painter->drawPixmap(QPointF(1704,856), m_pli[3]);
    painter->drawPixmap(QPointF(1772,856), m_pli[4]);
    QFont f2("Times New Roman");
    f2.setPointSize(30);
    f2.setBold(true);
    painter->setFont(f2);
    QImage test(":/images/Bulle Texte.png");
    if (m_bulles[1] != "")
    {
        painter->drawImage(1296,510,test);
        if (m_bulles[1][0] != 'J')
        {
            painter->drawText(QPointF(1336,568),"Je garde");
            painter->drawText(QPointF(1336,608),m_bulles[1]);
            painter->drawText(QPointF(1356,648),"chien");
        }
        else
        {
            painter ->drawText(QPointF(1336,598),m_bulles[1]);
        }
    }
    if (m_bulles[2] != "")
    {
        painter->drawImage(994,402,test.mirrored(false,true));
        if (m_bulles[2][0] != 'J')
        {
            painter->drawText(QPointF(1040,486),"Je garde");
            painter->drawText(QPointF(1040,526),m_bulles[2]);
            painter->drawText(QPointF(1060,566),"chien");
        }
        else
        {
            painter ->drawText(QPointF(1040,516),m_bulles[2]);
        }
    }
    if (m_bulles[3] != "")
    {
        painter->drawImage(761,300,test.mirrored(true,true));
        if (m_bulles[3][0] != 'J')
        {
            painter->drawText(QPointF(810,382),"Je garde");
            painter->drawText(QPointF(810,422),m_bulles[3]);
            painter->drawText(QPointF(830,462),"chien");
        }
        else
        {
            painter ->drawText(QPointF(810,412),m_bulles[3]);
        }
    }
    if (m_bulles[4] != "")
    {
        painter->drawImage(363,477,test.mirrored(true,false));
        if (m_bulles[4][0] != 'J')
        {
            painter->drawText(QPointF(413,530),"Je garde");
            painter->drawText(QPointF(410,570),m_bulles[4]);
            painter->drawText(QPointF(430,610),"chien");
        }
        else
        {
            painter ->drawText(QPointF(415,564),m_bulles[4]);
        }
    }
    QFont f3("Algerian");
    f3.setPointSize(60);
    painter->setFont(f3);
    painter->setPen(Qt::white);
    painter->drawText(QPointF(400,600), m_message);
    f3.setFamily("Georgia");
    f3.setPointSize(30);
    painter->setFont(f3);
    painter->drawText(QPointF(100,960), "Carte Appelée");
    painter->drawPixmap(QPointF(400,854), m_carteAppele);
}

void Cg_GrandTapis::carte_jouee(unsigned int joueur, Carte carte)
{
    Q_UNUSED(joueur)
    Q_UNUSED(carte)
    qDebug() << "carte_jouee - Cg_GrandTapis\n";
    m_message = "";
    update();
}

void Cg_GrandTapis::dernierPli(QVector<QPixmap> pli)
{
    unsigned int i = 0;
    for (i = 0 ; i < 5 ; i++)
    {
        m_pli[i] = pli[i];
    }
    update();
}

void Cg_GrandTapis::numero_change(unsigned int n)
{
    m_numeroJoueur = n;
    QString noms[5];
    unsigned int i = 0;
    for (i = 0 ; i<5 ; i++)
    {
        noms[i] = m_noms[(i+m_numeroJoueur)%5];
    }
    for (i = 0 ; i < 5 ; i++)
    {
        m_noms[i] = noms[i];
    }
    update();
}

void Cg_GrandTapis::adversaires(std::vector<std::string> noms)
{
    qDebug()<<"Adversaires\n";
    unsigned int i = 0;
    QPixmap carte2;
    for (i = 0 ; i < 5 ; i++)
    {
        m_pli[i] = QPixmap(carte2);
        m_bulles[i] = "";
    }
    for (i = 0 ; i<5 ; i++)
    {
        m_noms[i] = noms[i].c_str();
    }
    m_message = "";
    m_carteAppele = carte2;
    update();
}

void Cg_GrandTapis::pli_termine(unsigned int joueur)
{
    if (joueur == m_numeroJoueur)
    {
        m_message = "Vous avez remporté le pli !";
    }
    else
    {
        m_message = m_noms[(joueur - m_numeroJoueur + 5) % 5] + " a remporté le pli !";
    }
    update();
}

void Cg_GrandTapis::doit_jouer()
{
    m_message = "C'est à vous de jouer !";
    update();
}

void Cg_GrandTapis::maitre_change(unsigned int joueur)
{
    qDebug() << "maitre_change - Cg_GrandTapis\n";
    m_joueurMaitre = (joueur - m_numeroJoueur + 5) % 5;
    update();
}

void Cg_GrandTapis::contrat_intermediaire(Enchere e)
{
    qDebug()<<"Contrat Intermédiaire\n";
    QString annonce = "";
    switch (e.prise())
    {
        case Enchere::PASSE :
            annonce = "Je passe";
            break;
        case Enchere::PRISE :
            annonce = "Je prise";
            break;
        case Enchere::GARDE :
            annonce = "Je garde";
            break;
        case Enchere::GARDE_SANS :
            annonce = "sans le";
            break;
        case Enchere::GARDE_CONTRE :
            annonce = "contre le";
            break;
    }
    m_bulles[(e.joueur()- m_numeroJoueur + 5)%5] = annonce;
    update();
}

void Cg_GrandTapis::contrat_final(Enchere e)
{
    Q_UNUSED(e)
    unsigned int i = 0;
    for (i = 0 ; i < 5 ; i++)
    {
        m_bulles[i] = "";
    }
    m_carteAppele = QPixmap(creerChemin(e.carte_appelee()->numero()));
    update();
}

void Cg_GrandTapis::doit_identifier()
{
    emit formuler_identification(m_nomJoueur.toUtf8().constData());
    update();
}


